package com.home766.controller.terminal;

import com.home766.entity.Cases;
import com.home766.service.CasesService;
import net.biz.Page;
import net.biz.Pageable;
import net.biz.controller.shop.BaseController;
import net.biz.entity.ProductCategory;
import net.biz.service.ProductCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/terminal/cases")
public class TerminalCaseController extends BaseController {
	@Resource(name="casesServiceImpl")
	private CasesService casesService;
	@Resource(name = "productCategoryServiceImpl")
	private ProductCategoryService productCategoryService;

	/**
	 * 案列列表查询
	 * @param model
	 * @param pageable
	 * @return
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public String list(ModelMap model, Pageable pageable, Integer typeId){
		pageable.setPageSize(12);
		//只查如已发布等
		Cases cases = new Cases();
		cases.setIsRelease(true);
		if(typeId != null){
			cases.setProductCategory(typeId);
		}
		Page<Cases> page = casesService.getCasesList(cases,pageable,null);
		model.addAttribute("page", page);
		List<ProductCategory> listPc = productCategoryService.findRoots();
		model.addAttribute("listPc",listPc);
		model.addAttribute("typeId",typeId);
		return "/terminal/case/case_list";
	}
	
	/**
	 * 案列详情查看
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/content",method = RequestMethod.GET)
	public String content(ModelMap model, Long id){
		if(id == null){
			return TERMINAL_ERROR_VIEW;
		}
		Cases cases = casesService.find(id);
		if(cases == null){
			return TERMINAL_ERROR_VIEW;
		}
		model.addAttribute("cases", cases);
		return "/terminal/case/content";
	}
}
